
package com.jf.cloud.service.sysmanager.remote.api;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.constant.UserConstant;
import com.jf.cloud.service.sysmanager.constant.UserMatchConstant;
import com.jf.cloud.service.sysmanager.dao.IPubgClubService;
import com.jf.cloud.service.sysmanager.dao.IPubgMatchMsgService;
import com.jf.cloud.service.sysmanager.dao.IPubgMatchService;
import com.jf.cloud.service.sysmanager.dao.IPubgTeamService;
import com.jf.cloud.service.sysmanager.dao.IPubgUserMatchService;
import com.jf.cloud.service.sysmanager.dao.IPubgUserService;
import com.jf.cloud.service.sysmanager.entity.PubgClub;
import com.jf.cloud.service.sysmanager.entity.PubgMatch;
import com.jf.cloud.service.sysmanager.entity.PubgTeam;
import com.jf.cloud.service.sysmanager.entity.PubgUser;
import com.jf.cloud.service.sysmanager.entity.PubgUserMatch;
import com.jf.cloud.service.sysmanager.handler.TokenHandler;
import com.jf.cloud.service.sysmanager.support.entity.Result;
import com.jf.cloud.service.sysmanager.support.web.BaseController;

/**
 * 队员-赛事 消息 控制器
 * 
 * @author 朱建文 创建时间：2018年4月25日 下午7:13:12
 */
@Controller
@RequestMapping("/appMatchMsg")
public class AppPubgMatchMsgController extends BaseController {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(AppPubgMatchMsgController.class);

	/**
	 * 员工服务
	 */
	@Resource
	private IPubgMatchMsgService pubgMatchMsgService;

	@Resource
	private IPubgUserService pubgUserService;

	/**
	 * 战队服务
	 */
	@Resource
	private IPubgTeamService pubgTeamService;
	
	@Resource
	private IPubgClubService pubgClubService;
	
	@Resource
	private IPubgMatchService pubgMatchService;
	
	@Resource
	private IPubgUserMatchService pubgUserMatchService;
	
	@Resource
	private TokenHandler tokenHandler;

	/**
	 * 发送赛事消息
	 * 
	 * @author 朱建文 创建时间：2018年4月26日 下午1:31:26
	 */
	@ResponseBody
	@RequestMapping("/sendMatchMsg")
	public Result<String> sendMatchMsg(Long matchId, String sendContent, HttpServletRequest request){
		Result<String> result = new Result<String>();
		result.setCode(-1);
		result.setMessage("发送赛事消息失败！");
		try {
			
			// 验证token
			String token = getParamValueString(request, "token");
			String userId = tokenHandler.getCommUserInfoByToken(token);
			if (userId == null || userId.trim().length() == 0) {
				result.setMessage("请重新登陆！");
				return result;
			}
			
			// 验证操作者角色
			PubgUser pubgUser = this.pubgUserService.findByPK(Long.valueOf(userId));
			if (UserConstant.USER_ROLE_10.getId().equals(pubgUser.getUserRole())) {
				result.setMessage("您无权操作！");
				return result;
			}
			
			// 验证赛事
			PubgMatch pubgMatch = this.pubgMatchService.findByPK(matchId);
			if (pubgMatch == null) {
				result.setMessage("赛事不存在！");
				return result;
			}
			
			// 如果操作人为队长，验证所在战队是否可以举办该赛事
			if (UserConstant.USER_ROLE_20.getId().equals(pubgUser.getUserRole())) {
				Query query = new Query();
				query.put("_LIKE$CAPTAIN_ID", "," + userId + ",");
				PubgTeam pubgTeam = this.pubgTeamService.find(query);
				if (pubgTeam == null) {
					result.setMessage("您的战队角色有误！");
					return result;
				}
				
				if (pubgMatch.getMatchTeamId().indexOf(pubgTeam.getRowId() + ",") < 0) {
					result.setMessage("您的战队未举办该赛事！");
					return result;
				}
			}
			
			// 如果操作人为部长，验证操作人是否是该赛事的部长
			if (UserConstant.USER_ROLE_30.getId().equals(pubgUser.getUserRole())) {
				Query query = new Query();
				query.put("_LIKE$MINISTER_ID", "," + userId + ",");
				PubgClub pubgClub = pubgClubService.find(query);
				if (pubgClub == null) {
					result.setMessage("您的俱乐部角色有误！");
					return result;
				}
				
				if (!pubgMatch.getClubId().equals(pubgClub.getRowId())) {
					result.setMessage("您的俱乐部未举办该赛事！");
					return result;
				}
			}
			
			// 验证消息内容
			if (StringUtil.isBlank(sendContent)) {
				result.setMessage("发送内容为空！");
				return result;
			}
			
			// 插入消息
			boolean boo = this.pubgMatchMsgService.insertMatchMsg(pubgUser, matchId, sendContent);
			if (boo) {
				result.setCode(0);
				result.setMessage("发送赛事消息成功！");
			}
		} catch (Exception e) {
			log.error("发送赛事消息出错！", e);
		}
		return result;
	}

	/**
	 * 获取消息列表
	 * 
	 * @author 朱建文 创建时间：2018年4月26日 下午2:41:14
	 */
	@ResponseBody
	@RequestMapping("/getMatchMsg")
	public Result<List<Map<String, Object>>> getMatchMsg(Long matchId, HttpServletRequest request){
		Result<List<Map<String, Object>>> result = new Result<List<Map<String, Object>>>();
		result.setCode(-1);
		result.setMessage("获取消息列表失败！");
		try {

			// 验证token
			String token = getParamValueString(request, "token");
			String userId = tokenHandler.getCommUserInfoByToken(token);
			if (userId == null || userId.trim().length() == 0) {
				result.setMessage("请重新登陆！");
				return result;
			}
			
			// 验证赛事
			PubgMatch pubgMatch = this.pubgMatchService.findByPK(matchId);
			if (pubgMatch == null) {
				result.setMessage("赛事不存在！");
				return result;
			}
			
			// 验证队员-赛事状态  未通过的 不能获取消息
			PubgUser pubgUser = this.pubgUserService.findByPK(Long.valueOf(userId));
			if (UserConstant.USER_ROLE_10.getId().equals(pubgUser.getUserRole())) {
				Query query = new Query();
				query.put("USER_ID", userId);
				query.put("MATCH_ID", pubgMatch.getRowId());
				query.put("USER_STATE", UserMatchConstant.USER_MATCH_20.getId());
				List<PubgUserMatch> pubgUserMatchList = this.pubgUserMatchService.findList(query);
				if (pubgUserMatchList == null || pubgUserMatchList.isEmpty()) {
					result.setCode(0);
					result.setMessage("您还未通过审核！");
					return result;
				}
			}
			
			List<Map<String, Object>> dataList = this.pubgMatchMsgService.getMatchMsgList(matchId);
			result.setCode(0);
			result.setData(dataList);
			result.setMessage("获取消息列表成功！");
		} catch (Exception e) {
			log.error("获取消息列表出错！", e);
		}
		return result;
	}
}
